package com.moody.calculator;
/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-11-21 下午01:35:56
 * @Modified by: 计算LGD
 * @Description: 
 */

public class DebtRatingCalculator {
	//金融质押品标准违约损失率为45％
	public static Double financialLGD = 0.45;
	//金融类质押品
	public static final String fiancialCollateral = "0401";
	//应收账款
	public static final String receivableAccount  = "0402";
	//商用房地产和居住用房地产
	public static final String realEstate = "0403";
	//其他抵质押品
	public static final String otherCollateral = "0404";
	
	public DebtRatingCalculator(){
	}
	
	public DebtRatingCalculator(String debtQuality){
		//高级债
		if(debtQuality.equals("01")){
			financialLGD = 0.45;
		}
		//次级债
		else if(debtQuality.equals("02")){
			financialLGD = 0.75;
		}
			
			
	}
	
	/*
	 * 单项抵质押品的计算
	 */
	//type表示质押品类型编号，exposureValue为风险暴露的当前值， collateralValue为风险缓释前抵押品价值
	public Double calculateLGD(String type, Double exposureValue, Double collateralValue){
		Double LGD;
		
		//如果是金融类
		if(type.startsWith(fiancialCollateral)){
			if(exposureValue <= 0){
				LGD = 0.00;
			}else
				LGD = financialLGD*((exposureValue-collateralValue)/exposureValue);
		}
		//应收账款
		else if(type.startsWith(receivableAccount)){
			
//			//超额抵押
//			if(exposureValue<=0 || collateralValue/exposureValue >= 1.25)
//				LGD = 0.35;
//			//低额抵押
//			else if(collateralValue/exposureValue <= 0.0)
//				LGD = financialLGD;
//			//当 和 的比率介于最低抵押和 超额抵押之间时
//			else{
//			}
			if(exposureValue <= 0)LGD = 0.35;
			else{
				double percent = Math.max(collateralValue / exposureValue,0);
				LGD = Math.max(0.45 -  percent / 12.5,0.35) ;
			}
		}
		//商用房地产和居住用房地产
		else if(type.startsWith(realEstate)){
			//超额抵押
			if(exposureValue<=0 || collateralValue/exposureValue >= 1.4)
				LGD = 0.35;
			//低额抵押
			else if(collateralValue/exposureValue <= 0.3)
				LGD = financialLGD;
			//当 和 的比率介于最低抵押和 超额抵押之间时
			else{
//				LGD = ((collateralValue/1.25)*0.35+(exposureValue-collateralValue/1.25)*0.45)/exposureValue;
				LGD = 5.25/11 - collateralValue/(11 * exposureValue);
			}
		}
		//其他抵质押品
		else{
			//超额抵押
			if(exposureValue<=0 || collateralValue/exposureValue >= 1.4)
				LGD = 0.40;
			//低额抵押
			else if(collateralValue/exposureValue <= 0.3)
				LGD = financialLGD;
			//当 和 的比率介于最低抵押和 超额抵押之间时
			else{
				//LGD = ((collateralValue/1.25)*0.4+(exposureValue-collateralValue/1.25)*0.45)/exposureValue;
				LGD = 5.1/ 11 - 0.5* collateralValue/(exposureValue * 11);
			}
		}			
		
		return LGD;
	}
	
	public Double calculateEAD(){
		return 0.0;
	}
	
	public double  getFinancialLGD(){
		return financialLGD;
	}

}
